package com.app.newcsc.fragment.my.paytype.addpaytype;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.app.newcsc.BR;
import com.app.newcsc.R;
import com.app.newcsc.databinding.FragmentAddPaytypeBinding;
import com.app.newcsc.entity.Message;
import com.app.newcsc.utils.Utils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;

/**
 * @author: woking
 * @date: 2021/6/11
 * @description：
 */
public class AddPayTypeFragment extends BaseFragment<FragmentAddPaytypeBinding, BaseViewModel> {
    private Disposable mSubscription;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_add_paytype;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mSubscription != null)
        {
            RxSubscriptions.remove(mSubscription);
        }
    }
    @Override
    public void initData() {
        super.initData();
        if (getArguments() != null) {
            boolean hasBank = getArguments().getBoolean("hasBank");
            boolean hasZFB = getArguments().getBoolean("hasZFB");
            boolean hasWX = getArguments().getBoolean("hasWX");
            if(hasBank) binding.paytypeBankRl.setVisibility(View.GONE);
            if(hasZFB) binding.paytypeZfbRl.setVisibility(View.GONE);
            if(hasWX) binding.paytypeWxRl.setVisibility(View.GONE);
        }

        mSubscription = RxBus.getDefault().toObservable(Message.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(new Consumer<Message>() {
                    @Override
                    public void accept(final Message message) throws Exception {
                        Log.e("TAG","===============message=============="+message.getName());
                        if(message.getName().equals("update_pay_type"))
                        {
                            getActivity().finish();
                        }
                    }
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        binding.paytypeBankRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!Utils.isLogin())
                {
                    return;
                }
                startContainerActivity(AddBankFragment.class.getCanonicalName());
//                startContainerActivity(OrderDetailFragment.class.getCanonicalName());
//                startContainerActivity(GuaDanDetailFragment.class.getCanonicalName());
            }
        });
        binding.paytypeZfbRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!Utils.isLogin())
                {
                    return;
                }
                startContainerActivity(AddZFBFragment.class.getCanonicalName());
            }
        });
        binding.paytypeWxRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!Utils.isLogin())
                {
                    return;
                }
                startContainerActivity(AddWXFragment.class.getCanonicalName());
            }
        });
    }
}